<!DOCTYPE html>
<html>
<head>
    <title>发药--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/style.css"/>
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>
    <script type="text/javascript" src="../../Js/ckeditor/ckeditor.js"></script>


    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
        $(function () {
            $('#backid').click(function () {
                window.location.href = "dispensing.html";
            });
        });
    </script>
</head>
<body>
<form action="dispensing.html" method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">病人号</td>
            <td name="behId">1101,1102,1104</td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">姓名</td>
            <td name="behName">黄飞鸿,黄飞鸿,黄飞鸿</td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">药品名称</td>
            <td>
                <select name="drId">
                    <option>2304-九九感冒药</option>
                </select>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">发药数量</td>
            <td>
                <input type="text" name="assignDrugNum" value=""/>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <center>
                    <button class="btn btn-primary" type="button" id="save">保存</button> &nbsp;&nbsp;
                    <button type="button" class="btn btn-success" id="returnList">返回列表</button>
                </center>
            </td>
        </tr>
    </table>
</form>
</body>
<script type="text/javascript">

    String.prototype.GetValue= function(para) {
        let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?")+1).match(reg);
        if (r!=null) return unescape(r[2]); return null;
    }

    // 获取 url 中所有的 Key值
    // 无视 pageNum 参数
    String.prototype.GetKeys= function() {
        let ids = [];
        let hashes = this.substr(this.indexOf("\?")+1).split('&');
        for(let i = 1; i < hashes.length; i++){
            let hash = hashes[i].split('=');
            ids.push(hash[0]);
        }
        return ids;
    }

    // 获取 url 中所有的 Value值
    // 无视 pageNum 参数
    String.prototype.GetValues= function() {
        let names = [];
        let hashes = this.substr(this.indexOf("\?")+1).split('&');
        for(let i = 1; i < hashes.length; i++){
            let hash = hashes[i].split('=');
            names.push(decodeURIComponent(hash[1]));
        }
        return names;
    }

    let url = document.location.toString();
    let pageNum=url.GetValue("pageNum");
    let ids = url.GetKeys();
    let names = url.GetValues();

    $(function () {

        //首次加载信息
        loadPatient();
        loadDrug();

        //开药
        $('#save').click(function () {
            openDrug();
        });


    });

    //开药
    function openDrug() {
        // 添加其余数据
        let drId = $("select[name='drId']").val();
        let assignDrugNum = $("input[name='assignDrugNum']").val();

        let data = {
            "behIds":ids,
            "drId":drId,
            "assignDrugNum":assignDrugNum
        };
        console.log(data);

        if(confirm("对 "+names+" 病人进行开药？")){
            $.ajax({
                type: "POST",
                url: "/drugpeople/slapSendDrugs",
                data: data,
                traditional: true,
                dataType: "json",
                success: function (vo) {
                    if (vo.code !== 200){
                        alert("开药异常！"+vo.msg);
                    }
                    alert("开药成功！");
                    window.location.href = "dispensing.html?pageNum="+pageNum;
                }
            });
        }
    }

    //返回
    $('#returnList').click(function () {
        window.location.href = "dispensing.html?pageNum="+pageNum;
    });

    //病人信息加载
    function loadPatient() {
        $("td[name='behId']").html(ids.toString());
        $("td[name='behName']").html(names.toString());
    }

    //加载药物
    function loadDrug() {
        $.ajax({
            type : "get",
            url : "/drug/all",
            data : "",
            dataType : "json",
            success: function (vo) {
                let list = vo.list;
                let str = "<option value='0'>请选择药物</option>";
                for (let i = 0; i < list.length ; i++) {
                    let obj = list[i];
                    let drId = obj.drId;
                    let drName = obj.drFactory+" "+obj.drName;
                    str += "<option value='"+drId+"'>"+drId+" - "+drName+"</option>";
                }
                $("select[name='drId']").html(str);
            }
        });
    }


</script>
</html>